﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HCRS.DBACL;
using System.Text;
using Common;
using HCRS.Implementation;

namespace JinJiang.BookingEngine.Web.en
{
    public partial class BookingFIT : System.Web.UI.Page
    {
        DataAccessLayer _DataAccessLayer = new DataAccessLayer();
        Functions _Functions = new Functions();
        HCRS.Implementation.QueryXml _QueryXmlClass = new HCRS.Implementation.QueryXml();
        DataSet myDataSet;
        HotelClass _HotelClass = new HotelClass();
        //UserClass _UserClass = new UserClass();
        CommonWeb _CommonClass = new CommonWeb();
        string[] Day = new string[] { "Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat" };
        public static string ratecode = "";
        string domainurl, domain;

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(BookingFIT), this);
            try
            {

                int PID = Request.QueryString["p"] == null ? 999 : Convert.ToInt32(Request.QueryString["P"]);

                int adults = Request.QueryString["A"] == null ? 1 : Convert.ToInt32(Request.QueryString["A"]);
                if (adults > 4) { adults = 4; }
                int room = Request.QueryString["R"] == null ? 1 : Convert.ToInt32(Request.QueryString["R"]);
                if (room > 5) { room = 5; }

                string checkIN = Request.QueryString["I"] == null ? "" : _CommonClass.VerifySQL(Request.QueryString["I"]);
                string checkOUT = Request.QueryString["O"] == null ? "" : _CommonClass.VerifySQL(Request.QueryString["O"]);

                ratecode = Request.QueryString["C"];
                if (DataAcl.Common.checkexpojs(ratecode))
                    googlejs.Visible = true;

                if (ratecode == null || ratecode == "")
                {
                    chkall.Checked = true;
                    chkall.Attributes.Add("style", "display:none");
                }

                txtDepTime.Text = _CommonClass.getDate(checkOUT);
                txtStartTime.Text = _CommonClass.getDate(checkIN);

                //页面使用的主要日期格式为用户在当前页选择的日期.如:2007-sept-01;如果用户使用的是页面传值,则日期为2007-9-1
                //所以在页面开始加载时就将其统一转换维 2007-sept-01  
                //
                checkIN = _CommonClass.getDate(checkIN);
                checkOUT = _CommonClass.getDate(checkOUT);
                dropAdult.Text = adults.ToString();
                dropRoom.Text = room.ToString();
                hdfpropertyID.Value = PID.ToString();



                csscontrel.InnerHtml = _HotelClass.GetCSSContrel(PID, 1);
                string[] hotel = _DataAccessLayer.RunSqlStrings("select title_e,Stars,photourl,lowprice,highprice,pid,address1,currency,photourl from [hotel] where PID=" + PID + " ");
                if (hotel != null && hotel.Length > 0)
                {
                    hdfpropertyID.Value = PID.ToString();
                    Header.Title = hotel[0] + " reservation system-step1-check rates";
                }
                else
                {
                    Response.Write("Err,Can't Find Hotel Infomation");
                    Header.Title = "HUBS1 reservation system-step1-check rates";

                }
                //domain = _HotelClass.GetDomain(PID, 1);

                if (Request.UrlReferrer != null)
                    domain = FGBCommon.WebDomain(false, out domainurl);

                if (checkOUT != "" && checkIN != "" && PID > 0 && adults > 0 && room > 0)
                {
                    bodyBooking.Attributes.Add("onload", "fSetOutLinkIframe(\"" + domain + "\");JinJiang.BookingEngine.Web.en.BookingFIT.CheckRates('" + checkIN + "','" + checkOUT + "','" + PID.ToString() + "','" + adults.ToString() + "','0','" + room.ToString() + "',document.getElementById('chkall').checked,'" + domainurl + "',Hotel)");
                }
                else
                {
                    bodyBooking.Attributes.Add("onload", "fSetOutLinkIframe(\"" + domain + "\")");
                }
                p1.InnerHtml = "<script language=\"javascript\" type=\"text/javascript\" defer=\"defer\">";

                p1.InnerHtml += "function SearchHotel()";
                p1.InnerHtml += "{";

                p1.InnerHtml += " var hdfpropertyID=document.getElementById(\"hdfpropertyID\").value;";
                p1.InnerHtml += " var child= document.getElementById(\"dropChild\").value;";
                p1.InnerHtml += "var checkOUT= document.getElementById(\"txtDepTime\").value;";
                p1.InnerHtml += "var adult= document.getElementById(\"dropAdult\").value;";
                p1.InnerHtml += "var room= document.getElementById(\"dropRoom\").value;";
                p1.InnerHtml += "var checkIN= document.getElementById(\"txtStartTime\").value;";

                p1.InnerHtml += "if (DateValidate()) {";
                p1.InnerHtml += "JinJiang.BookingEngine.Web.en.BookingFIT.CheckRates(checkIN,checkOUT,hdfpropertyID,adult,child,room,document.getElementById('chkall').checked,'" + domainurl + "',Hotel);";
                p1.InnerHtml += "}";
                p1.InnerHtml += "}";

                p1.InnerHtml += "function Hotel(request)";
                p1.InnerHtml += "{";
                p1.InnerHtml += "var str=request.value;";
                p1.InnerHtml += "document.getElementById(\"divRate\").innerHTML=str;";
                p1.InnerHtml += "fSetOutLinkIframe(\"" + domain + "\");";
                p1.InnerHtml += "}";
                p1.InnerHtml += "AjaxPro.timeoutPeriod = 60000;";

                p1.InnerHtml += "AjaxPro.onLoading = function(b) {";
                p1.InnerHtml += "var l = document.getElementById(\"loadinfo\");";
                p1.InnerHtml += "l.style.display = b ? \"block\" : \"none\";";
                p1.InnerHtml += "var le = document.getElementById(\"loadErrinfo\");";
                p1.InnerHtml += "le.style.display = \"none\";";
                p1.InnerHtml += "var lt = document.getElementById(\"loadTimeoutinfo\");";
                p1.InnerHtml += "lt.style.display = \"none\";";
                //l.innerHTML=AjaxPro.queue.queue.length 

                p1.InnerHtml += "} ";
                p1.InnerHtml += "\n";
                p1.InnerHtml += "AjaxPro.onError = function(e) {";
                p1.InnerHtml += "var le = document.getElementById(\"loadErrinfo\");";
                p1.InnerHtml += "le.style.display = e ? \"block\" : \"none\";";
                p1.InnerHtml += "}";
                p1.InnerHtml += "\n";
                p1.InnerHtml += "AjaxPro.onTimeout = function(t) {";
                p1.InnerHtml += "var lt = document.getElementById(\"loadTimeoutinfo\");";
                p1.InnerHtml += "lt.style.display = t ? \"block\" : \"none\";";
                p1.InnerHtml += "}";
                p1.InnerHtml += "</script>";

                //Resetiframe.setScript(PID);
            }
            catch (Exception showe)
            {
                HCRS.Errors.WebError.SendErrorEmail(showe, "", HttpContext.Current.Request.Cookies["Hubs1user"]["UserName"].ToString(), "订房宝");
                Response.Write("Err,Please contact the web admin");

            }
        }

        [AjaxPro.AjaxMethod]
        public string CheckRates(string Checkindate, string Checkoutdate, int propertyID, int adults, int chileren, int room, bool allprice, string domainurltemp)
        {

            string errmsg;
            string responsHtml = "";
            TimeSpan ts;

            ts = Convert.ToDateTime(_CommonClass.getDate(Checkoutdate)) - Convert.ToDateTime(_CommonClass.getDate(Checkindate));
            Checkindate = _CommonClass.getDate(Checkindate);//格式化日期
            Checkoutdate = _CommonClass.getDate(Checkoutdate);//格式化日期
            int nights = Convert.ToInt32(ts.TotalDays);
            //bool showGuestInfo = true;
            //string plandesc;
            //string planstatus;
            //string roomdesc;
            //string roomname;
            //string ratedesc;
            //string ratename;
            //string responsexml;
            string roomType = "";
            string rateClass = "";

            EnSearchHotelClass myHotelClass = new EnSearchHotelClass();

            if (!allprice)//不是所有价格
            {
                if (!myHotelClass.RateCodeAvailable(propertyID, "pid", ratecode, out rateClass))
                {
                    return "No rates for this hotel at this moment（Error 001）";
                }
            }
            else
            {
                myHotelClass.RateCodeAvailable(propertyID, "pid", "", out rateClass);
            }

            if (rateClass == "" || rateClass == null)
            {
                return "No rates for this hotel at this moment（Error 002）";
            }

            //_QueryXmlClass.GetRateMap(roomType, rateClass, Convert.ToDateTime(Checkindate).ToString("yyyy-MM-dd"), propertyID, nights, adults, chileren, room, 1, out  errmsg, out myDataSet);
            GetRateInfo info = new GetRateInfo();
            info.adults = adults;
            info.checkin = Checkindate;
            info.children = chileren;
            //info.currencycode = "";
            //info.filter = "0";
            info.iata = CommonFun.IATA();
            info.language = 1;
            info.nights = nights;
            info.pid = propertyID;
            info.ratecodes = rateClass.Replace("|", ",").Split(',');
            //info.roomcodes = roomType.Split(',');
            info.rooms = room;

            myDataSet = OrderClass.getexratemap(info, out errmsg);

            if (errmsg == "Error" || errmsg == "noResponse")
            {
                responsHtml = "No rates for this hotel at this moment.";
            }

            else if (errmsg == "noEnoughAvailRoom")
            {
                responsHtml = "No availbility for this hotel at this moment.";

            }
            else if (errmsg == "")
            {
                H_ratelist _H_ratelist = new H_ratelist();
                string url = "Reservation.aspx?HotelID={0}&RoomCode={1}&RateCode={2}&ChkIN={3}&ChkOUT={4}&Rooms={5}&Adult={6}&Child={7}&wd=" + System.Web.HttpUtility.UrlEncode(domainurltemp);
                responsHtml += _H_ratelist.FIT_GetXMLRate(myDataSet, propertyID, Checkindate, Checkoutdate, nights, room, adults, chileren, url, 1);

            }
            else
            {

                responsHtml = "Can't connect to Central Reservation System temporary,please try later.";
            }
            return responsHtml;
        }
    }
}